<?php


namespace addons\ymypaylinx\library\paylinx;


use fast\Random;
use think\Request;

class Alipay extends Base
{
    // app支付链接
    protected $app_url = 'http://online.paylinx.cn/alipay/gateway/app/';
    // 扫码支付链接
    protected $scan_url = 'https://paylinx.cn/alipay/gateway/create/';
    // web支付链接
    protected $web_url = 'http://online.paylinx.cn/alipay/gateway/web/';
    // h5支付链接
    protected $h5_url = 'http://online.paylinx.cn/alipay/gateway/h5/';
    // 汇率接口
    protected $exchagerate_url = 'http://online.paylinx.cn/alipay/gateway/queryexchagerate/';
    // 订单状态
    protected $orderquery_url = 'http://online.paylinx.cn/alipay/gateway/orderquery/';
    // 申请退款
    protected $refund_url = 'http://online.paylinx.cn/alipay/gateway/refund/';
    // 查询退款
    protected $refund_find_url = 'https://paylinx.cn/wxpay/gateway/refundquery/';
    public function __construct(array $items = [])
    {
        parent::__construct($items);
    }

    //APP支付
    public function app($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->app_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result['requestParams'];
        }
        throw new \Exception("请求异常");
    }

    //扫码支付
    public function scan($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->scan_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);

        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result['pic_url'];
        }
        throw new \Exception("请求异常");
    }

    //web支付
    public function web($params){
        $getQuery = $this->buildParams($params,'get');
        $url = $this->web_url.'?'.$getQuery;
        return $url;
    }

    //h5支付
    public function h5($params){
        $payload = $this->buildParams($params,'form');
        $html = $this->buildPayHtml($this->h5_url,$payload,'POST');
        return $html;
    }

    //查询订单
    public function find($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->orderquery_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result;
        }
        throw new \Exception("请求异常");
    }


    //查询汇率
    public function exchangeRate($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->exchagerate_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            return 5;
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            return 5;
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result['rate'];
        }
        return 5;
    }

    //申请退款
    public function refund($params){
        $xmlQuery = $this->buildParams($params);
        $resultXml = $this->httpsRequest($this->refund_url,$xmlQuery);
        $result = $this->xmlToArray($resultXml);
        if(isset($result['err_code_des'])){
            throw new \Exception($result['err_code_des']);
        }elseif((isset($result['return_code']) && $result['return_code']=='FAILD') || (isset($result['result_code']) && $result['result_code']=='FAILD')){
            throw new \Exception($result['return_msg']??'请求失败');
        }elseif(isset($result['result_code']) && $result['result_code']=='SUCCESS' && isset($result['return_code']) && $result['return_code']=='SUCCESS'){
            return $result;
        }
        throw new \Exception("请求异常");
    }


    //构建请求参数
    protected function buildParams($params,$type = 'xml'){
        if(isset($params['total_fee'])){
            $params['total_fee'] = $params['total_fee'] * 100;
        }
        $baseData = [
            'partner' => $this->mch_id,
            'store_id' => $this->store_id,
            'refer_url' => $this->refer_url,
            'notify_url' => $this->notify_url,
            'trans_currency' => 'AUD',
            'body' => '商城支付'
        ];
        $data = array_merge($baseData,$params);
     //   file_put_contents('test.txt',json_encode($data,JSON_UNESCAPED_UNICODE));
        $sign = $this->sign($data);
        $data['sign'] = $sign;
        if($type == 'get'){
            $str = http_build_query($data);
            return $str;
        }elseif($type == 'form'){
            return $data;
        }
        $query = $this->arrayToXml($data);
        return $query;
    }

    //构建h5支付表单
    protected function buildPayHtml($endpoint, $payload, $method = 'POST')
    {


        $sHtml = "<form id='alipay_submit' name='alipay_submit' action='".$endpoint."' method='".$method."'>";
        foreach ($payload as $key => $val) {
            $val = str_replace("'", '&apos;', $val);
            $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
        }
        $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
        $sHtml .= "<script>document.forms['alipay_submit'].submit();</script>";

        return $sHtml;
    }
}